package client.ui.view.panel.good;

import client.entity.Good;
import client.ui.component.edittext.InputArea;
import client.ui.theme.ThisFont;
import client.ui.util.MaterialBorder;
import client.ui.util.MaterialColors;
import client.ui.view.base.PanelControl;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import java.awt.*;

/**
  * @Description 描述面板
  * @author 孔明浩
  * @Date 2020/3/31
  */
public class GoodDescriptionPanel extends JPanel implements PanelControl {
    private Good goodInfo;
    private JLabel priceLabel;
    private InputArea describePanel;
    private JLabel support;
    public GoodDescriptionPanel(Good good){
        this.goodInfo = good;
        initial();
    }
    private void initial(){
        this.setLayout(new BorderLayout());
        this.setBackground(MaterialColors.WHITE);
        this.setBorder(new LineBorder(MaterialColors.COSMO_BLUE, 2));
        priceLabel = new JLabel("价格:￥" + goodInfo.getPrice() + "  剩余:" + goodInfo.getCount());
        priceLabel.setBorder(new EmptyBorder(12,12,4,12));
        priceLabel.setFont(ThisFont.BIG_BOLD_FONT);
        priceLabel.setForeground(MaterialColors.COSMO_RED);
        priceLabel.setOpaque(true);
        JButton button = new JButton();
        button.setPreferredSize(new Dimension(0,0));
        describePanel = new InputArea(null,button,null);
        describePanel.setText(goodInfo.getDescription());
        describePanel.setEditable(false);
        describePanel.setInputAreaFont(ThisFont.MIDDLE_FONT);
        support = new JLabel();
        if (goodInfo.getAuctionId() == 0){
            support.setText("此商品不支持拍卖");
        } else {
            support.setText("此商品支持拍卖");
        }
        support.setBorder(new LineBorder(MaterialColors.COSMO_RED));
        support.setFont(ThisFont.LARGE_BOLD_FONT);
        support.setForeground(MaterialColors.COSMO_BLUE);
        support.setBackground(MaterialColors.WHITE);
        this.add(priceLabel, BorderLayout.NORTH);
        this.add(describePanel, BorderLayout.CENTER);
        this.add(support, BorderLayout.SOUTH);
    }

    @Override
    public void updatePanel(Object object) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                if (object instanceof Good){
                    goodInfo = (Good) object;
                    //根据信息判断是否可以拍买
                    if (goodInfo.getAuctionId() == 0){
                        support.setText("此商品不支持拍卖");
                    } else {
                        support.setText("此商品支持拍卖");
                    }
                    describePanel.setText(goodInfo.getDescription());
                    priceLabel.setText("价格:￥" + goodInfo.getPrice() + "  剩余:" + goodInfo.getCount());
                    priceLabel.setToolTipText("价格:￥" + goodInfo.getPrice() + "  剩余:" + goodInfo.getCount());
                }
            }
        });

    }
}
